package io.renren.modules.dishes.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.io.IOException;
import java.util.List;

@Data
public class SpuVo {
    private String spuId;
    private String spuName;
    private String spuImage;
    private String recommendReason;
    private DishesDetail dishesDetail;
    private NutritionalContent nutritionalContent;
    private String classifyId;

    @Data
    public static class DishesDetail {
        private String eaterial;
        private String taste;
        @JsonProperty("cooking_methods")
        private List<String> cookingMethods;

        public String toJsonString() throws JsonProcessingException {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.writeValueAsString(this);
            }catch (Exception e) {
                e.printStackTrace();
                return null;
            }

        }
    }

    @Data
    public static class NutritionalContent {
        private List<Nutrient> data;
        private String totalHeat;

        public String toJsonString() throws JsonProcessingException {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.writeValueAsString(this);
            }catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    @Data
    public static class Nutrient {
        private int value;
        private String name;
        public String toJsonString() throws JsonProcessingException {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.writeValueAsString(this);
            }catch (Exception e) {
                e.printStackTrace();
                return null;
            }

        }
    }

    public static DishesDetail fromJsonStringToDishesDetail(String jsonString) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(jsonString, DishesDetail.class);
    }

    // 将JSON字符串转换为NutritionalContent对象
    public static NutritionalContent fromJsonStringToNutritionalContent(String jsonString) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(jsonString, NutritionalContent.class);
    }
}
